package com.hrbxlh.action;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xwpf.model.XMLParagraph;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;















import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.log.SysoCounter;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter2;

public class ItextManager {
	
	 	private Font font;
	 	private Font font1;
	 	private Font font2;
	    private BaseFont bfChinese;  
	  
	    public ItextManager() throws Exception {  
	        // 设置中文字体  
	        bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 
	        //每道题样式
	        font = new Font(bfChinese);  
	        font.setSize(15);  
	        font.setStyle(FontFactory.HELVETICA);
	        font.setColor(new BaseColor(0,0,0)); 
	        //整套试卷样式
	        font1 = new Font(bfChinese);  
	        font1.setSize(30);  
	        font1.setStyle(FontFactory.HELVETICA);  
	        font1.setStyle(Font.BOLD);//加粗  
	        font1.setColor(new BaseColor(0,0,0));
	        //每道题标题样式
	        font2 = new Font(bfChinese);  
	        font2.setSize(15);  
	        font2.setStyle(FontFactory.HELVETICA);  
	        font2.setStyle(Font.BOLD);//加粗  
	        font2.setColor(new BaseColor(0,0,0));
	          
	    }  
	  
	    public static ItextManager getInstance() throws Exception {  
	        return new ItextManager();  
	    }  
	  
		public void createRtfContext(Map<String,Object> resultMap, OutputStream out,String type,Map<String,Object> paramMap) {  
	        Document doc = new Document(PageSize.A4, 20, 20, 20, 20); 
	       //选择集合
			List<Map<String, Object>> optionList = (List<Map<String, Object>>) resultMap.get("optionList");
			//判断集合
			List<Map<String, Object>> judgeList = (List<Map<String, Object>>) resultMap.get("judgeList");
			//填空集合
			List<Map<String, Object>> blanksList = (List<Map<String, Object>>) resultMap.get("blanksList");
			//简答集合
			List<Map<String, Object>> answerList = (List<Map<String, Object>>) resultMap.get("answerList");
			//课程内容
			Map<String, Object> courseMap = (Map<String, Object>) resultMap.get("courseMap");
			//内容计数
			int contentCount=0;
			//标题计数
			int titleCount=0;
			System.out.println("选择"+optionList);
			System.out.println("判断"+judgeList);
			System.out.println("填空"+blanksList);
			System.out.println("简答"+answerList);
			System.out.println("课程"+courseMap);
			//分数
			float optionSorce = 0;
			float judgeSource = 0;
			float blanksSource = 0;
			float answerSource = 0;
	        try {  
	            if("word".equals(type)){ 
	            	
//	            	RtfWriter2.getInstance(doc, out);
//	            	PdfWriter writer = PdfWriter.getInstance(doc, out);
	            	
	            }else if("pdf".equals(type)){  
	                PdfWriter.getInstance(doc, out);  
	            }  
	            doc.open();  
	            Paragraph title1 = null; 
	            if(courseMap!=null){
	            	//选择大标题
	                String titleStr = (String) courseMap.get("courseName")+"试题";
	                System.out.println("什么课程啊"+titleStr);
	                // 标题  
	                Paragraph title = new Paragraph(titleStr, font1); 
	                title.setAlignment(Element.CCITT_BLACKIS1);  
	                doc.add(title);
	                // 换行  
	                title1 = new Paragraph("\n");  
	                doc.add(title1); 
	            }
	            
	            if(optionList!=null&&optionList.size()!=0){
	            	//选择取标题
	                Map<String,Object> titleMap = (Map<String, Object>) resultMap.get("titleMap"); 
	                if((String)titleMap.get("optionTitle")!=""&&titleMap.get("optionTitle")!=null){
	                	titleCount+=1;
	                	//算分数
	                	optionSorce = Float.parseFloat(String.valueOf(paramMap.get("optionSorce")))*(optionList.size());
	                	String titleSorce ="(每题"+String.valueOf(paramMap.get("optionSorce"))+"分,共"+optionSorce+"分)";
	                	String titleName = "一."+(String)titleMap.get("optionTitle")+titleSorce;
	                	 // 标题  
		                Paragraph title = new Paragraph(titleName, font2); 
		                title.setAlignment(Element.ALIGN_LEFT);  
		                doc.add(title);
		                // 换行  
		                title1 = new Paragraph("\n");  
		                doc.add(title1); 
	                }
			                 
			            
	            	//循环选择集合
		            for (int i = 0; i < optionList.size(); i++) {  
		                Map<String,Object> questionMap = optionList.get(i);
		                // 正文  
		                int countI = i+1;
		                String name = String.valueOf(countI)+"."+(String)questionMap.get("questionName");
		                Paragraph questionName = new Paragraph(name, font);  
		                questionName.setAlignment(Element.ALIGN_LEFT);  
		                doc.add(questionName); 
		                String optionMsg = (String)questionMap.get("optionMsg");
//		                optionMsg = optionMsg.replaceAll("<br>", "\n");
		                optionMsg = optionMsg.replaceAll("<br>", "\0");
		                System.out.println("选项跑哪去了"+optionMsg);
		                String[] optionMsgArr = new String[4];
		                if(optionMsg.contains("，")){
		                	optionMsgArr = optionMsg.split("，");
		                } if(optionMsg.contains(",")){
		                	optionMsgArr = optionMsg.split(",");
		                }
		                for(int j=0;j<optionMsgArr.length;j++){
		                	System.out.println("optionMsgArr[j]"+optionMsgArr[j]);
		                	String optionMsgArrStr="";
		                	if(optionMsgArr[j]!=null){
		                		optionMsgArrStr = optionMsgArr[j].trim();
		                	}else{
		                		 optionMsgArrStr = optionMsgArr[j];
		                	}
		                	Paragraph finalOptionMsg = new Paragraph(optionMsgArrStr, font);
		                	finalOptionMsg.setAlignment(Element.ALIGN_LEFT); 
			                doc.add(finalOptionMsg);
		                }
		                // 换行  
		                title1 = new Paragraph("\n");  
		                doc.add(title1);
		                contentCount = countI;
//		                System.out.println("循环选择后计数为"+contentCount);
		  
		            }
	            }
	            
	            if(judgeList!=null&&judgeList.size()!=0){
	            	System.out.println("判断"+titleCount);
	            	//判断取标题
	        		 Map<String,Object> titleMap = (Map<String, Object>) resultMap.get("titleMap"); 
		                if((String)titleMap.get("judgeTitle")!=""&&titleMap.get("judgeTitle")!=null){
		                	String titleName = "";
		                	//算分数
		                	judgeSource = Float.parseFloat(String.valueOf(paramMap.get("judgeSource")))*(judgeList.size());
		                	String titleSorce ="(每题"+String.valueOf(paramMap.get("judgeSource"))+"分,共"+judgeSource+"分)";
		                	if(titleCount>0){
		                		//说明有选择题
		                		titleName = "二."+(String)titleMap.get("judgeTitle")+titleSorce;
		                	}else{
		                		//说明没有选择题
		                		titleName = "一."+(String)titleMap.get("judgeTitle")+titleSorce;
		                	}
		                	 // 标题  
			                Paragraph title = new Paragraph(titleName, font2); 
			                title.setAlignment(Element.ALIGN_LEFT);  
			                doc.add(title);
			                // 换行  
			                title1 = new Paragraph("\n");  
			                doc.add(title1);  
			                titleCount+=1;
		                }
			           
	            	//循环判断集合
		            for (int i = 0; i < judgeList.size(); i++) {
//		            	System.out.println("循环判断前计数为"+contentCount);
		                Map<String,Object> questionMap = judgeList.get(i);
//		                int countI = i+1;
		                contentCount+=1;
		                String name = String.valueOf(contentCount)+"."+(String)questionMap.get("questionName");
		                // 正文  
		                Paragraph questionName = new Paragraph(name, font);  
		                questionName.setAlignment(Element.ALIGN_LEFT);  
		                doc.add(questionName); 
		                // 换行  
		                title1 = new Paragraph("\n");  
		                doc.add(title1);
//		                System.out.println("循环判断后计数为"+contentCount);
		                
		            } 
	            }
	            
	            if(blanksList!=null&&blanksList.size()!=0){
	            	System.out.println("填空"+titleCount);
	            	//填空取标题
            		 Map<String,Object> titleMap = (Map<String, Object>) resultMap.get("titleMap"); 
		                if((String)titleMap.get("blanksTitle")!=""&&titleMap.get("blanksTitle")!=null){
		                	String titleName = "";
		                	//算分数
		                	blanksSource = Float.parseFloat(String.valueOf(paramMap.get("blanksSource")))*(blanksList.size());
		                	String titleSorce ="(每题"+String.valueOf(paramMap.get("blanksSource"))+"分,共"+blanksSource+"分)";
		                	if(titleCount>0&&titleCount>1){
		                		//说明既有选择题又有判断题、
		                		titleName = "三."+(String)titleMap.get("blanksTitle")+titleSorce;
		                	}if(titleCount>0&&titleCount<=1){
		                		//说明前面只有一道题、
		                		titleName = "二."+(String)titleMap.get("blanksTitle")+titleSorce;
		                	}if(titleCount==0){
		                		//说明前面一道题也没有、
		                		titleName = "一."+(String)titleMap.get("blanksTitle")+titleSorce;
		                	}
		                	 // 标题  
			                Paragraph title = new Paragraph(titleName, font2); 
			                title.setAlignment(Element.ALIGN_LEFT);  
			                doc.add(title);
			                // 换行  
			                title1 = new Paragraph("\n");  
			                doc.add(title1); 
			                titleCount+=1;
		                }
		                 
			            
	            	//循环填空集合
		            for (int i = 0; i < blanksList.size(); i++) {  
//		            	System.out.println("循环填空前计数为"+contentCount);
		                Map<String,Object> questionMap = blanksList.get(i);
//		                int countI = i+1;
		                contentCount+=1;
		                String name = String.valueOf(contentCount)+"."+(String)questionMap.get("questionName");
		                // 正文  
		                Paragraph questionName = new Paragraph(name, font);  
		                questionName.setAlignment(Element.ALIGN_LEFT);  
		                doc.add(questionName); 
		  
		                // 换行  
		                title1 = new Paragraph("\n");  
		                doc.add(title1);  
//		                System.out.println("循环填空后计数为"+contentCount);
		  
		            } 
	            }
	            
	            if(answerList!=null&&answerList.size()!=0){
	            	System.out.println("简答"+titleCount+"&&&&&&&"+resultMap.get("titleMap"));
	            	//循环简答集合取标题
	                Map<String,Object> titleMap = (Map<String, Object>) resultMap.get("titleMap"); 
	                if((String)titleMap.get("answerTitle")!=""&&titleMap.get("answerTitle")!=null){
	                	String titleName = "";
	                	//算分数
	                	answerSource = Float.parseFloat(String.valueOf(paramMap.get("answerSource")))*(answerList.size());
	                	String titleSorce ="(每题"+String.valueOf(paramMap.get("answerSource"))+"分,共"+answerSource+"分)";
	                	if(titleCount>=3){
	                		//说明既有选择题又有判断题还有填空题、
	                		titleName = "四."+(String)titleMap.get("answerTitle")+titleSorce;
	                	}if(titleCount>0&&titleCount<=1){
	                		//说明前面只有一道题、
	                		titleName = "二."+(String)titleMap.get("answerTitle")+titleSorce;
	                	}if(titleCount==0){
	                		//说明前面一道题也没有、
	                		titleName = "一."+(String)titleMap.get("answerTitle")+titleSorce;
	                	}if(titleCount>=2&&titleCount<3){
	                		System.out.println("没走这吗"+(String)titleMap.get("answerTitle"));
	                		//说明前面有两道题、
	                		titleName = "三."+(String)titleMap.get("answerTitle")+titleSorce;
	                	}
	                	 // 标题  
		                Paragraph title = new Paragraph(titleName, font2); 
		                title.setAlignment(Element.ALIGN_LEFT);  
		                doc.add(title);
		                // 换行  
		                title1 = new Paragraph("\n");  
		                doc.add(title1); 
		                titleCount+=1;
	                }
			                
	            	//循环简答集合
		            for (int i = 0; i < answerList.size(); i++) {  
//		            	System.out.println("循环简答前计数为"+contentCount);
		                Map<String,Object> questionMap = answerList.get(i); 
//		                int countI = i+1;
		                contentCount+=1;
		                String name = String.valueOf(contentCount)+"."+(String)questionMap.get("questionName");
		                // 正文  
		                Paragraph questionName = new Paragraph(name, font);  
		                questionName.setAlignment(Element.ALIGN_LEFT);  
		                doc.add(questionName);
		  
		                // 换行  
		                title1 = new Paragraph("\n");  
		                doc.add(title1);  
//		                System.out.println("循环简答后计数为"+contentCount);
		  
		            } 
	            }
	            
	            doc.close();  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	    }  
	  
	    /** 
	     * 第一种解决方案 在不改变图片形状的同时，判断，如果h>w，则按h压缩，否则在w>h或w=h的情况下，按宽度压缩 
	     *  
	     * @param h 
	     * @param w 
	     * @return 
	     */  
	  
	    public static int getPercent(float h, float w) {  
	        int p = 0;  
	        float p2 = 0.0f;  
	        if (h > w) {  
	            p2 = 297 / h * 100;  
	        } else {  
	            p2 = 210 / w * 100;  
	        }  
	        p = Math.round(p2);  
	        return p;  
	    }  
	  
	    /** 
	     * 第二种解决方案，统一按照宽度压缩 这样来的效果是，所有图片的宽度是相等的，自我认为给客户的效果是最好的 
	     *  
	     * @param args 
	     */  
	    public static int getPercent2(float h, float w) {  
	        int p = 0;  
	        float p2 = 0.0f;  
	        p2 = 530 / w * 100;  
	        p = Math.round(p2);  
	        return p;  
	    }  
	
	
}
